<template>
  <div class="LabelManagement">
    <el-card>
      <h3><span class="tag"></span>标签管理</h3>
      <div class="head">
        <el-button @click="add">新增</el-button>
        <el-button class="btn" @click="submit">保存</el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%; margin-top: 10px"
        :header-cell-style="{ background: '#f5f7fa' }"
      >
        <el-table-column prop="sort" label="排序">
          <template slot-scope="scope">
            <el-input v-model="scope.row.sort"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="标签名称">
          <template slot-scope="scope">
            <el-input v-model="scope.row.name"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="icon" label="图标">
          <template slot-scope="scope">
            <!-- <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.icon"
              :preview-src-list="[scope.row.icon]"
            >
            </el-image> -->
            <div @click="upIcon(scope.row)">
              <el-upload
                class="avatar-uploader"
                :http-request="upLoad"
                :show-file-list="false"
                action
              >
                <div class="imgbox" v-if="scope.row.icon">
                  <img :src="scope.row.icon" class="avatar" />
                </div>
                <i v-else class="upicon el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button
              size="small"
              @click="del(scope.row.id, scope.$index)"
              plain
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 增改弹窗 -->
    <!-- <el-dialog :title="title" :visible.sync="show" width="30%" center>
      <el-form label-position="right" label-width="80px" :model="form">
        <el-form-item label="序号">
          <el-input
            placeholder="前输入序号"
            onkeyup="this.value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')"
            v-model="form.sort"
          ></el-input>
        </el-form-item>
        <el-form-item label="标签名称">
          <el-input placeholder="请输入标签名称" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-upload
            class="avatar-uploader"
            :http-request="upLoad"
            :show-file-list="false"
            action
          >
            <div class="imgbox" v-if="form.icon">
              <img :src="form.icon" class="avatar" />
            </div>
            <i v-else class="upicon el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="show = false">取 消</el-button>
        <el-button size="small" class="btn" type="primary" @click="submit"
          >确 定</el-button
        >
      </span>
    </el-dialog> -->
  </div>
</template>

<script>
import {
  configQueryLiveBroadcastTagList,
  configAddLiveBroadcastTag,
} from "@/api/operationManagement";
import { client } from "@/utils/oss.js";
import { ossToken } from "@/api/NotificationManagement";
export default {
  data() {
    return {
      loading: false,
      // title: "",
      tableData: [],
      // show: false,
      // form: {
      //   name: "",
      //   icon: "",
      //   sort: "",
      // },
      targetObj: {},
    };
  },
  methods: {
    async submit() {
      // this.show = false;
      const res = await configAddLiveBroadcastTag({
        data: this.tableData,
      });
      if (res.code == 0) {
        this.$message.success("保存成功！");
        this.getList();
      }
    },
    del(id, index) {
      this.tableData.splice(index, 1);
    },
    add() {
      // this.title = "新增标签";
      // this.form = {
      //   name: "",
      //   icon: "",
      //   sort: "",
      // };
      // this.show = true;
      this.tableData.push({
        name: "",
        icon: "",
        sort: "",
      });
    },
    upIcon(obj) {
      this.targetObj = obj;
    },
    async getList() {
      this.loading = true;
      const res = await configQueryLiveBroadcastTagList();
      this.loading = false;
      if (res.code == 0) {
        this.tableData = res.data;
      }
    },
    //上传
    async upLoad(file) {
      await ossToken({
        type: 0,
      }).then((res) => {
        if (res.code == 0) {
          this.dataObj = {
            accessKeyId: res.data.accessKey,
            accessKeySecret: res.data.accessSecret,
            securityToken: res.data.token,
            region: "oss-" + res.data.regionId,
            bucket: res.data.bucket,
          };
        }
      });
      const self = this;
      let fileNames =
        "/img/" +
        Math.round(new Date() / 1000) +
        "." +
        file.file.name.substring(file.file.name.lastIndexOf(".") + 1);
      console.log(fileNames);
      client(this.dataObj)
        .multipartUpload(fileNames, file.file)
        .then((result) => {
          //下面是如果对返回结果再进行处理，根据项目需要
          this.targetObj.icon = "https://img.psychexy.com" + fileNames;
          // var images = new Image();
          // images.src = this.form.icon.path;
          // images.onload = function () {
          //   console.log("宽度为：", this.width, "高度为：", this.height);
          //   self.form.icon.width = this.width;
          // self.form.icon.height = this.height;
          self.$message({
            message: "上传成功",
            type: "success",
          });
          // };
        })
        .catch((err) => {
          self.$message.error("上传失败");
        });
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style lang="less" scoped>
.LabelManagement {
  padding: 20px;
  h3 {
    margin: 0 0 10px;
    display: flex;
    align-items: center;
    .tag {
      display: inline-block;
      width: 5px;
      height: 18px;
      background-color: #7b94d8;
      margin-right: 10px;
    }
  }
  .head {
    text-align: right;
    // display: flex;
    // justify-content: space-between;
  }
  .upicon {
    display: inline-block;
    width: 50px;
    height: 50px;
    line-height: 50px;
    font-size: 24px;
    border: 1px dashed #ccc;
    border-radius: 8px;
  }
  .imgbox {
    position: relative;
    .avatar {
      display: inline-block;
      width: 50px;
      height: 50px;
      line-height: 50px;
      font-size: 24px;
      border: 1px dashed #ccc;
      border-radius: 8px;
      padding: 2px;
    }
    i {
      position: absolute;
      top: -10px;
      right: 40px;
      font-size: 24px;
      font-weight: 700;
      color: #000;
    }
  }
}
</style>